﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CROSLAND.BL.BE;
using System.Data.Common;
using System.Data;

namespace CROSLAND.DL.DALC.MSSQL
{
    public class CondicionDALC : BaseDALC, ICondicionDALC
    {
        #region ICondicionDALC Members

        public List<CondicionBE> ObtenerCondiciones(PedidoBE objPedido,
                DetalleBE objDetalle, out string retval)
        {
            List<CondicionBE> entitys = new List<CondicionBE>();

            DbCommand command = dbProvider.GetStoredProcCommand("USPR_OBTENER_CONDICIONES");
            
            dbProvider.AddInParameter(command, "AUART", DbType.String, objPedido.CodTipoDoc);
            dbProvider.AddInParameter(command, "KUNNR", DbType.String, objPedido.CodCliente);
            dbProvider.AddInParameter(command, "SPART", DbType.String, objPedido.Sector);
            dbProvider.AddInParameter(command, "VKORG", DbType.String, objPedido.OrgVentas);
            dbProvider.AddInParameter(command, "VTWEG", DbType.String, objPedido.CanalDist);
            dbProvider.AddInParameter(command, "MATNR", DbType.String, objDetalle.CodMaterial);
            dbProvider.AddInParameter(command, "FECHA", DbType.String, ParametroDALC.dateNetToSap(objPedido.Fecha));
            dbProvider.AddInParameter(command, "CHARG", DbType.String, objDetalle.CodLote);
            dbProvider.AddInParameter(command, "ZTERM", DbType.String, objPedido.CondPago);
            dbProvider.AddInParameter(command, "KDKGR", DbType.String, objPedido.GrupoCond);
            dbProvider.AddInParameter(command, "KDGRP", DbType.String, "");
            //dbProvider.AddInParameter(command, "VKBUR", DbType.String, objPedido.CodOficina);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    CondicionBE entity = new CondicionBE();
                    entity.ClaseCondicion = dr["KSCHL"].ToString();
                    entity.Denominacion = dr["VTEXT"].ToString();

                    if (objPedido.Sector == "00")
                    {
                        entity.Tratamiento = "D";
                    }
                    else
                    {
                        if (entity.ClaseCondicion == "YD02" || entity.ClaseCondicion == "YD03" || entity.ClaseCondicion == "YD04")
                        {
                            entity.Tratamiento = "D";
                        }
                    }

                    entity.LimiteInferior = Convert.ToDecimal(dr["MXWRT"].ToString());

                    entitys.Add(entity);
                }

            }
            retval = null;

            return entitys;
        }

        #endregion
    }
}
